package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import team.seekers.skill.entity.dao.Question;
import team.seekers.skill.entity.dao.User;
import team.seekers.skill.entity.query.QuestionQuery;
import team.seekers.skill.entity.vo.admin.list.AdminQuestionListVO;
import team.seekers.skill.entity.vo.fore.list.QuestionListVO;
import team.seekers.skill.mapper.QuestionMapper;
import team.seekers.skill.service.QuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.skill.common.utils.SkillUtils;
import team.seekers.skill.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-02
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    private UserService userService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean removeBySectionId(String sectionId){
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("section_id",sectionId);
        boolean remove = remove(wrapper);
        return remove;
    }

    @Override
    public Map<String, Object> pageQuestionCondition(long current, long limit, QuestionQuery questionQuery) {
        Page<Question> page = new Page<>(current,limit);
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(questionQuery.getSectionId())){
            wrapper.eq("section_id",questionQuery.getSectionId());
        }
        if(!StringUtils.isEmpty(questionQuery.getSkillId())){
            wrapper.eq("skill_id",questionQuery.getSkillId());
        }
        if(!StringUtils.isEmpty(questionQuery.getTitle())){
            wrapper.like("title",questionQuery.getTitle());
        }
        if(questionQuery.getIsAnswered() != null){
            wrapper.eq("is_answered",questionQuery.getIsAnswered());
        }
        wrapper.orderByDesc("update_date");
        page(page,wrapper);
        //总共的记录条数
        long total = page.getTotal();
        //所有的记录
        List<Question> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<QuestionListVO> list = new ArrayList<>();
        for (Question record : records) {
            QuestionListVO questionVO = new QuestionListVO();
            BeanUtils.copyProperties(record,questionVO);
            if(SkillUtils.getUserId().equals(record.getUserId())){
                questionVO.setMe(true);
            }
            if(record.getIsAnswered() == 1){
                questionVO.setAnswered(true);
            }
            User byId = userService.getById(record.getUserId());
            if(byId == null){
                return null;
            }
            questionVO.setAvatar(byId.getAvatar());
            questionVO.setNickname(byId.getNickname());
            list.add(questionVO);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

}
